# coding: utf-8
from PySide6.QtCore import Qt
from qfluentwidgets import SingleDirectionScrollArea
from common import SingleIcon
from components import FramelessWindow, SlidingStackedWidget
from .data_interface import DataInterface
from .tree_data_interface import TreeDataInterface


class MainInterface(FramelessWindow):
    def __init__(self):
        super().__init__()
        self.scrollArea = SingleDirectionScrollArea(self)
        self.stackedWidget = SlidingStackedWidget(self)
        self.dataInterface = DataInterface(self)
        self.treeDataInterface = TreeDataInterface(self)

        self.__initWidgets()

    def __initWidgets(self):
        self.setWindowTitle('青书学堂')
        self.setWindowIcon(SingleIcon.LOGO.icon())

        self.scrollArea.setWidget(self.stackedWidget)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.scrollArea.enableTransparentBackground()
        self.scrollArea.setGeometry(0, 45, self.width(), self.height() - 45)

        self.stackedWidget.addWidget(self.dataInterface)
        self.stackedWidget.addWidget(self.treeDataInterface)
        self.stackedWidget.setCurrentIndex(0)

        self.dataInterface.treeDataSignal.connect(self.onTreeDataSignal)

    def resizeEvent(self, event):
        self.scrollArea.setGeometry(0, 45, self.width(), self.height() - 45)
        super().resizeEvent(event)

    def onTreeDataSignal(self, tree_data:dict):
        self.treeDataInterface.setData(tree_data)
        self.stackedWidget.setCurrentWidget(self.treeDataInterface)